package cn.xsw.service.impl;

import cn.xsw.entity.Comment;
import cn.xsw.entity.query.CommentQuery;
import cn.xsw.entity.vo.CommentVo;
import cn.xsw.mapper.CommentMapper;
import cn.xsw.service.CommentService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liangYiHui
 * @since 2021-06-03
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Resource
    private UserServiceImpl userService;
    @Override
    public IPage findPageVo(CommentQuery query) {
        Page page = page(query.page(),query.wrapper());
        List<Comment> comments = (List<Comment>)page.getRecords();
        List<CommentVo> commentVos = new ArrayList<CommentVo>();
        comments.forEach(c->{
            CommentVo commentVo = new CommentVo();
            BeanUtils.copyProperties(c,commentVo);
            commentVo.setUser(userService.getById(c.getUid()));
            commentVos.add(commentVo);
        });
        page.setRecords(commentVos);
        return page;
    }
}
